/*
 * TaskStateMachineControl.java
 *
 * Created on 04 February 2004, 08:26
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui.node.taskstatemachine;

import com.velocityme.client.gui.ChangeDeltaPanel;
import com.velocityme.client.gui.exception.ExceptionWindow;
import com.velocityme.client.gui.node.taskstatemachine.StateMachineControl;
import com.velocityme.client.utility.StatelessRemoteClientSessionUtil;
import com.velocityme.entity.PermissionBean;
import com.velocityme.interfaces.KeySession;
import com.velocityme.interfaces.NodePK;
import com.velocityme.interfaces.NodeUtil;
import com.velocityme.interfaces.PermissionPK;
import com.velocityme.interfaces.TaskStateMachinePK;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.utility.PermissionDeniedException;
import com.velocityme.valueobjects.RoleValue;
import com.velocityme.valueobjects.TaskStateMachineValue;
import com.velocityme.valueobjects.TransitionValue;
import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Set;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.NamingException;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;

/**
 *
 * @author  Robert Crida Work
 */
public class TaskStateMachineControl extends javax.swing.JPanel {
    
    private TaskStateMachinePK m_taskStateMachinePK;
    private NodePK m_parentNodePK;
    private KeySession m_key;
    private TaskStateMachineValueDisplay m_taskStateMachineValueDisplay;
    private boolean m_editing = true;
    private Set m_setPermissions;
    private TransitionValue[] m_transitionValues = null;
    private RoleValue[] m_allRoleValues = null;
    
    /** Creates new form TaskStateMachineControl */
    public TaskStateMachineControl(KeySession p_key, TaskStateMachinePK p_taskStateMachinePK, boolean p_editing, Set p_setPermissions) {
        initComponents();
        m_key = p_key;
        m_taskStateMachinePK = p_taskStateMachinePK;
        m_editing = p_editing;
        m_setPermissions = p_setPermissions;
        try {
            if (!p_editing)
                updateTransitionValues();
            else
                m_allRoleValues = StatelessRemoteClientSessionUtil.getSession().getAllRoleValues(m_key);
            m_taskStateMachineValueDisplay = new TaskStateMachineValueDisplay(StatelessRemoteClientSessionUtil.getSession().getTaskStateMachineValue(m_key, m_taskStateMachinePK), m_transitionValues, m_allRoleValues);
            if (!p_editing) {
                jTabbedPane1.addTab("Change Log", new ChangeDeltaPanel(m_key, new NodePK(m_taskStateMachinePK.getTaskStateMachineId())));
                jButtonEdit.setEnabled(m_setPermissions.contains(new PermissionPK(new Integer(PermissionBean.TASK_TYPE_EDIT))));
                jButtonStateMachine.setEnabled(true);
            }
            else {
                jButtonOK.setEnabled(true);
                jButtonCancel.setEnabled(true);
            }
            m_taskStateMachineValueDisplay.setEditable(p_editing);
            jPanel3.add(m_taskStateMachineValueDisplay, java.awt.BorderLayout.CENTER);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(PermissionDeniedException e) {
            JOptionPane.showMessageDialog(this, "Permission denied!", "View Task Type", JOptionPane.WARNING_MESSAGE);
        }
    }
    
    /** Creates new form TaskStateMachineControl */
    public TaskStateMachineControl(KeySession p_key, NodePK p_parentNodePK) {
        initComponents();
        m_key = p_key;
        m_parentNodePK = p_parentNodePK;
        try {
            m_allRoleValues = StatelessRemoteClientSessionUtil.getSession().getAllRoleValues(m_key);
            m_taskStateMachineValueDisplay = new TaskStateMachineValueDisplay(new TaskStateMachineValue(), null, m_allRoleValues);
            m_taskStateMachineValueDisplay.setEditable(true);
            jPanel3.add(m_taskStateMachineValueDisplay, java.awt.BorderLayout.CENTER);        
            jButtonOK.setEnabled(true);
            jButtonCancel.setEnabled(true);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
    }
    
    private void updateTransitionValues() throws RemoteException, NamingException, CreateException {
        try {
            m_transitionValues = StatelessRemoteClientSessionUtil.getSession().getTaskStateMachineValue(m_key, m_taskStateMachinePK).getTransitionValues();
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
        catch(PermissionDeniedException e) {
            JOptionPane.showMessageDialog(this, "Permission denied!", "View Transitions", JOptionPane.WARNING_MESSAGE);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jButtonEdit = new javax.swing.JButton();
        jButtonOK = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();
        jButtonStateMachine = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        jPanel3.setLayout(new java.awt.BorderLayout());

        jPanel3.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                jPanel3ComponentShown(evt);
            }
        });

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jButtonEdit.setText("Edit");
        jButtonEdit.setEnabled(false);
        jButtonEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonEdit);

        jButtonOK.setText("OK");
        jButtonOK.setEnabled(false);
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonOK);

        jButtonCancel.setText("Cancel");
        jButtonCancel.setEnabled(false);
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonCancel);

        jButtonStateMachine.setText("State Machine");
        jButtonStateMachine.setEnabled(false);
        jButtonStateMachine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonStateMachineActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonStateMachine);

        jPanel3.add(jPanel2, java.awt.BorderLayout.SOUTH);

        jTabbedPane1.addTab("Task Type", jPanel3);

        add(jTabbedPane1, java.awt.BorderLayout.CENTER);

    }//GEN-END:initComponents

    private void jButtonStateMachineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonStateMachineActionPerformed
        // Add your handling code here:
        StateMachineControl changePanel = new StateMachineControl (m_key, m_taskStateMachinePK);
        ((JTabbedPane)getParent()).addTab("Edit Task State Machine", changePanel);
        ((JTabbedPane)getParent()).setSelectedComponent(changePanel);                                       
    }//GEN-LAST:event_jButtonStateMachineActionPerformed

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        // Add your handling code here:
        getParent().remove(this);                
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
        // Add your handling code here:
        try {
            TaskStateMachineValue taskStateMachineValue = m_taskStateMachineValueDisplay.getTaskStateMachineValue();
            if (taskStateMachineValue.taskStateMachineIdHasBeenSet())
                StatelessRemoteClientSessionUtil.getSession().taskStateMachineEdit(m_key, m_taskStateMachinePK, taskStateMachineValue);
            else
                StatelessRemoteClientSessionUtil.getSession().taskStateMachineCreate(m_key, m_parentNodePK, taskStateMachineValue);
            getParent().remove(this);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(PermissionDeniedException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }        
    }//GEN-LAST:event_jButtonOKActionPerformed

    private void jButtonEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditActionPerformed
        // Add your handling code here:
        TaskStateMachineControl editPanel = new TaskStateMachineControl(m_key, m_taskStateMachinePK, true, m_setPermissions);
        ((JTabbedPane)getParent()).addTab("Edit Task Type", editPanel);
        ((JTabbedPane)getParent()).setSelectedComponent(editPanel);        
    }//GEN-LAST:event_jButtonEditActionPerformed

    private void jPanel3ComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jPanel3ComponentShown
        // Add your handling code here:
        try {
            if (!m_editing) {
                updateTransitionValues();
                m_taskStateMachineValueDisplay.setTaskStateMachineValue(StatelessRemoteClientSessionUtil.getSession().getTaskStateMachineValue(m_key, m_taskStateMachinePK), m_transitionValues, m_allRoleValues);
            }
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(PermissionDeniedException e) {
            JOptionPane.showMessageDialog(this, "Permission denied!", "View Task Type", JOptionPane.WARNING_MESSAGE);
        }        
    }//GEN-LAST:event_jPanel3ComponentShown
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonEdit;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JButton jButtonStateMachine;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables
    
}
